package com.tech.koufu.ui.adapter;

import java.util.ArrayList;

import com.lidroid.xutils.http.client.multipart.content.ContentBody;
import com.tech.koufu.R;
import com.tech.koufu.model.StockInfo;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.ui.adapter.MarketAdapter.ViewHolder;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class StocksGridItemAdapter extends BaseAdapter {
	
	public ArrayList<StockInfo> datas;
	private Context context;  
	private LayoutInflater mInflater = null; 
	public StocksGridItemAdapter(Context context,ArrayList<StockInfo> datas) { 
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.datas = datas;
	}

	@Override
	public int getCount() {  
		return datas.size();
	}

	@Override
	public Object getItem(int position) { 
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;   
         if(convertView == null)  
         {  
             holder = new ViewHolder();   
             convertView = mInflater.inflate(R.layout.lv_choose_stocks_item, parent,false);  
             holder.txt_stocks_name = (TextView)convertView.findViewById(R.id.txt_stocks_name); 
             holder.txt_sc_increase_value = (TextView)convertView.findViewById(R.id.txt_sc_increase_value);
             holder.txt_sc_neweast_price = (TextView)convertView.findViewById(R.id.txt_sc_neweast_price);  
             holder.vbot = (View)convertView.findViewById(R.id.vbot);
             holder.vtop = (View)convertView.findViewById(R.id.vtop);
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
         StockInfo stockInfo = (StockInfo) getItem(position);  
         holder.txt_stocks_name.setText(stockInfo.getName()+"("+stockInfo.getCode()+")");
         String iv = String.format(context.getString(R.string.sc_increase_value), stockInfo.getZg());
         SpannableStringBuilder style=new SpannableStringBuilder(iv);          
         style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.textColorGray_888888)),0,4,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
         style.setSpan(new AbsoluteSizeSpan(26) , 0, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
         holder.txt_sc_increase_value.setText(style);  
         
//         String[] zspt = stockInfo.getStocksMarketsValue(stockInfo.getZx(), stockInfo.getZsp());
//         String nsp = "0";
//         if(zspt[1].equals("1")){
//        	 nsp =  zspt[1];
//         }else if(zspt[0].equals("0")){
//        	 nsp = zspt[1];
//         }
         String nsp = stockInfo.getNstocksMarketsValue(stockInfo.getZx(), stockInfo.getZsp());
         String zx = String.format(context.getString(R.string.sc_neweast_price_top), stockInfo.getZx(),nsp);
         style=new SpannableStringBuilder(zx);          
         style.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.textColorGray_888888)),0,4,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
         style.setSpan(new AbsoluteSizeSpan(26) , 0, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
         if(nsp!=null && nsp.startsWith("-")){
        	 style.setSpan(new ForegroundColorSpan(context.getResources().getColor(
        			 KouFuTools.getStockValueColor(nsp))),
        			 zx.length()-1-nsp.length(),
        			 zx.length()-1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
        	  
         }
         
         holder.txt_sc_neweast_price.setText(style); 
         
         if(position==getCount()-1 || position == getCount()-2){
        	 holder.vbot.setVisibility(View.VISIBLE);
         }
      
		return convertView;
	}
	 
	static class ViewHolder  
    {    
        public TextView txt_stocks_name;   
        public TextView txt_sc_increase_value;   
        public TextView txt_sc_neweast_price; 
        public View vtop,vbot;
        
    }  
}
